<!--
 * @Descripttion: (库区信息/fac_whse)
 * @Author: (admin)
 * @Date: (2025-04-20)
-->
<template>
	<div>
		<el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
			<el-form-item label="库区编码" prop="whseCode">
				<el-input v-model="queryParams.whseCode" placeholder="请输入库区编码" />
			</el-form-item>
			<el-form-item label="库区名称" prop="whseName">
				<el-input v-model="queryParams.whseName" placeholder="请输入库区名称" />
			</el-form-item>
			<el-form-item label="所属工厂" prop="factoryId">
				<el-select clearable v-model="queryParams.factoryId" placeholder="请选择工位类型">
					<el-option v-for="item in options.factoryList" :key="item.factoryId" :label="item.factoryName"
						:value="item.factoryId">
						<span class="fl">{{ item.factoryName }}</span>
						<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.factoryCode }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="启用标志" prop="enabled">
				<el-select clearable v-model="queryParams.enabled" placeholder="启用标志">
					<el-option v-for="item in options.enabledOptions" :key="item.dictValue" :label="item.dictLabel"
						:value="item.dictValue">
						<span class="fl">{{ item.dictLabel }}</span>
						<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.dictValue }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
				<el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="15" class="mb10">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['fac:whse:add']" plain icon="plus" @click="handleAdd">
					{{ $t('btn.add') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['fac:whse:edit']" plain icon="edit"
					@click="handleUpdate">
					{{ $t('btn.edit') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['fac:whse:delete']" plain icon="delete"
					@click="handleDelete">
					{{ $t('btn.delete') }}
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
			highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="whseId" label="库区id" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('whseId')" />
			<el-table-column prop="factoryId" label="所属工厂id" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('factoryId')" />
			<el-table-column prop="factoryInfo" label="所属工厂" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('factoryInfo')" />
			<el-table-column prop="whseCode" label="库区编码" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('whseCode')" />
			<el-table-column prop="whseName" label="库区名称" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('whseName')" />
			<el-table-column prop="enabled" label="启用标志" align="center" v-if="columns.showColumn('enabled')">
				<!-- （0禁用1启用） -->
				<template #default="scope">
					<dict-tag :options="options.enabledOptions" :value="scope.row.enabled" />
				</template>
			</el-table-column>
			<el-table-column label="修改记录" width="90" align="center" show-overflow-tooltip
				v-if="columns.showColumn('updateRecord')">
				<template #default="scope">
					<modifyRecord :data="scope.row" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" width="150">
				<template #default="scope">
					<el-button-group>
						<el-button text icon="edit" @click="handleUpdate(scope.row)" v-hasPermi="['fac:whse:edit']">
							{{ $t('btn.edit') }}
						</el-button>
						<el-button text icon="delete" @click="handleDelete(scope.row)" v-hasPermi="['fac:whse:delete']">
							{{ $t('btn.delete') }}
						</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
			@pagination="getList" />
		<el-dialog draggable :close-on-click-modal="false" :title="title" :lock-scroll="false" v-model="open">
			<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20">
					<!-- <el-col :lg="12">
						<el-form-item label="库区id" prop="whseId">
							<el-input v-model="form.whseId" placeholder="请输入库区id" :disabled="opertype != 1" />
						</el-form-item>
					</el-col> -->
					<el-col :lg="12">
						<el-form-item label="库区编码" prop="whseCode">
							<el-input v-model="form.whseCode" placeholder="请输入库区编码" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="库区名称" prop="whseName">
							<el-input v-model="form.whseName" placeholder="请输入库区名称" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="所属工厂" prop="factoryId">
							<el-select clearable v-model="form.factoryId" placeholder="请选择工位类型" style="width: 100%;">
								<el-option v-for="item in options.factoryList" :key="item.factoryId" :label="item.factoryName"
									:value="item.factoryId">
									<span class="fl">{{ item.factoryName }}</span>
									<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.factoryCode }}</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="启用标志" prop="enabled">
							<el-radio-group v-model="form.enabled">
								<el-radio v-for="item in options.enabledOptions" :key="item.dictValue"
									:value="parseInt(item.dictValue)">
									{{ item.dictLabel }}
								</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="opertype != 3">
				<el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
				<el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="facwhse">
import modifyRecord from '@/components/MyTable/modifyRecord.vue';
import { listFacFactory } from '@/api/facManage/facFactory.js'
import {
	pageFacWhse,
	addFacWhse, delFacWhse,
	updateFacWhse, getFacWhse,
}
	from '@/api/facManage/facWhse.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	sort: '',
	sortType: 'asc',
	whseCode: undefined,
	whseName: undefined,
	enabled: undefined,
	factoryId: undefined,
})
const columns = ref([
	{ visible: false, prop: 'whseId', label: '库区id' },
	{ visible: true, prop: 'whseCode', label: '库区编码' },
	{ visible: true, prop: 'whseName', label: '库区名称' },
	{ visible: true, prop: 'enabled', label: '启用标志' },
	{ visible: false, prop: 'factoryId', label: '所属工厂id' },
	{ visible: true, prop: 'factoryInfo', label: '所属工厂' },
	{ visible: true, prop: 'updateRecord', label: '修改记录', },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList() {
	loading.value = true
	pageFacWhse(queryParams).then(res => {
		const { code, data } = res
		if (code == 200) {
			dataList.value = data.result
			total.value = data.totalNum
			loading.value = false
		}
	})
}

// 查询
function handleQuery() {
	queryParams.pageNum = 1
	getList()
}

// 重置查询操作
function resetQuery() {
	proxy.resetForm("queryRef")
	handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map((item) => item.whseId);
	single.value = selection.length != 1
	multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
	var sort = undefined
	var sortType = undefined

	if (column.prop != null && column.order != null) {
		sort = column.prop
		sortType = column.order

	}
	queryParams.sort = sort
	queryParams.sortType = sortType
	handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
	single: true,
	multiple: true,
	form: {},
	rules: {
		whseId: [{ required: true, message: "库区id不能为空", trigger: "blur" }],
		whseCode: [{ required: true, message: "库区编码不能为空", trigger: "blur" }],
		whseName: [{ required: true, message: "库区名称不能为空", trigger: "blur" }],
		enabled: [{ required: true, message: "启用标志不能为空", trigger: "change", type: "number" }],
		factoryId: [{ required: true, message: "所属工厂不能为空", trigger: "change" }],
	},
	options: {
		// 启用标志（0禁用1启用） 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0',listClass:'success'}
		enabledOptions: [
			{ dictLabel: '禁用', dictValue: '0', listClass: 'danger' },
			{ dictLabel: '启用', dictValue: '1', listClass: 'success' }
		],
		// 工厂信息
		factoryList: []
	}
})

const { form, rules, options, single, multiple } = toRefs(state)

//获取工厂信息
function getFactoryList() {
	listFacFactory({}).then(res => {
		const { code, data } = res
		if (code == 200) {
			state.options.factoryList = data
		}
	})
}

// 关闭dialog
function cancel() {
	open.value = false
	reset()
}

// 重置表单
function reset() {
	form.value = {
		whseId: null,
		whseCode: null,
		whseName: null,
		enabled: null,
		factoryId: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null,
	};
	proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
	reset();
	open.value = true
	title.value = '添加库区信息'
	opertype.value = 1
	form.value.whseId = proxy.parseGuid();
}
// 修改按钮操作
function handleUpdate(row) {
	reset()
	const id = row.whseId || ids.value
	getFacWhse(id).then((res) => {
		const { code, data } = res
		if (code == 200) {
			open.value = true
			title.value = '修改库区信息'
			opertype.value = 2

			form.value = {
				...data,
			}
		}
	})
}

// 添加&修改 表单提交
function submitForm() {
	proxy.$refs["formRef"].validate((valid) => {
		if (valid) {

			if (form.value.whseId != undefined && opertype.value === 2) {
				updateFacWhse(form.value).then((res) => {
					proxy.$modal.msgSuccess("修改成功")
					open.value = false
					getList()
				})
			} else {
				addFacWhse(form.value).then((res) => {
					proxy.$modal.msgSuccess("新增成功")
					open.value = false
					getList()
				})
			}
		}
	})
}

// 删除按钮操作
function handleDelete(row) {
	const Ids = row.whseId ? [row.whseId] : undefined || ids.value
	proxy
		.$confirm('是否确认删除"' + Ids.length + '"条数据项？', "警告", {
			confirmButtonText: proxy.$t('common.ok'),
			cancelButtonText: proxy.$t('common.cancel'),
			type: "warning",
		})
		.then(function () {
			return delFacWhse(Ids)
		})
		.then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		})
}


handleQuery()
getFactoryList()
</script>